package com.web.remark;

import com.bean.Remark;
import com.bean.User;
import com.service.RemarkService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class DisplayRemarkServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session=req.getSession();
        RemarkService service=new RemarkService();

        List<Remark> remark=service.getAllremark();
        session.setAttribute("remark",remark);

        //身份判断，游客显示过滤后，博主显示原始评论
        User user=(User) session.getAttribute("user");
        if(user==null){
            try {

                int articleId=(int)session.getAttribute("articleId");
                List<Remark> singleRemark=service.getSingleRemark(articleId);
                req.getSession().setAttribute("singleRemark",singleRemark);

                int num=service.findAllCount(articleId);
                req.getSession().setAttribute("remarkNum",num);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            req.getRequestDispatcher("/DisplayReply").forward(req, resp);
        }else{
            req.getRequestDispatcher("/admin/remark/RemarkList.jsp").forward(req, resp);
        }

    }
}
